package com.linkstec.queue;
import java.util.Comparator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

//矩形类
class Node{
  public Node(int priority, String name)
  {
	  this.priority=priority;
      this.name=name;
  }
  int priority;
  String name;
}


public class PriorityBlockingQueue_test {

    public static void main(String args[]){
    //自定义比较类，先比较长，长升序排列，若长相等再比较宽，宽降序
        Comparator<Node> cNode =new Comparator<Node>() {
            public int compare(Node o1, Node o2) {
                if(o1.priority!=o2.priority){
                	return o1.priority-o2.priority;
                }
				return 0;
            }
            
        };

        //定义一个优先队列,用来排序任意两点之间的距离，从大到小排
        BlockingQueue<Node> q = new PriorityBlockingQueue<Node>(10,cNode);

        Node n1=new Node(4, "测试1");
        Node n2=new Node(3, "测试2");
        Node n3=new Node(2, "测试3");
        Node n4=new Node(2, "测试4");
        Node n5=new Node(2, "测试5");
        Node n6=new Node(5, "测试6");
        Node n7=new Node(1, "测试7");
        q.add(n1);
        q.add(n2);
        q.add(n3);
        q.add(n4);
        q.add(n5);
        q.add(n6);
        q.add(n7);
        Node n = null;
        while(!q.isEmpty())
        {
			try {
				n=q.take();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
            System.out.println("节点: "+n.name);
        }
    }
}